﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace 畜牧业审核程序国产.Models
{
    //通过json保存用户填写的账号信息
    internal class AppSettings
    {
        public string Str国网Account { get; set; }
        public string Str国网Password { get; set; }
        public string Str省网Account { get; set; }
        public string Str省网Password { get; set; }
        public string StrCloudAccount { get; set; }
        public string StrCloudPassword { get; set; }

        public string LedgerPath { get; set; }
        public string LedgerFolderPath { get; set; }
    }
    internal class SettingManager
    {
        //private readonly string _settingFilePath;
        //private const string SettingsFileName = "appsettings.json";
        //private AppSettings _settings;
        //public SettingManager()
        //{
        //    string JsonPath = Path.Combine(MyDataFolderOperator.AuditProgramFolderPath, "json文件夹");
        //    _settingFilePath = Path.Combine(_settingFilePath, SettingsFileName);
        //    LoadSettings();
        //}
        private const string SettingsFileName = "appsettings.json";//不指定路径的话就保存在当前目录下
        private AppSettings _settings;
        public SettingManager()
        {
            LoadSettings();
        }

        private void LoadSettings()
        {
            if (File.Exists(SettingsFileName))
            {
                var json = File.ReadAllText(SettingsFileName);
                _settings = JsonSerializer.Deserialize<AppSettings>(json);
            }
            else
            {
                _settings = new AppSettings(); // 可以设置默认值
            }
        }
        public string Str国网Account => _settings.Str国网Account;
        public string Str国网Password => _settings.Str国网Password;
        public string Str省网Account => _settings.Str省网Account;
        public string Str省网Password => _settings.Str省网Password;
        public string StrCloudAccount => _settings.StrCloudAccount;
        public string StrCloudPassword => _settings.StrCloudPassword;
        public string LedgerPath => _settings.LedgerPath;
        public string LedgerFolderPath => _settings.LedgerFolderPath;
        public void SaveSettings()
        {
            var json = JsonSerializer.Serialize(_settings);
            File.WriteAllText(SettingsFileName, json);
        }
        public void UpdateAccountInfo(string 国网account,string 国网password,string 省网account,string 省网password,string Cloudaccount,string Cloudpassword)
        {
            _settings.Str国网Account = 国网account;
            _settings.Str国网Password = 国网password;
            _settings.Str省网Account = 省网account;
            _settings.Str省网Password = 省网password;
            _settings.StrCloudAccount = Cloudaccount;
            _settings.StrCloudPassword = Cloudpassword;
            SaveSettings(); // 可选，自动保存
        }

        public void UpdateLedgerPath(string ledgerPath,string ledgerFolderPath)
        {
            if  (!string.IsNullOrWhiteSpace( ledgerPath))
            {
                _settings.LedgerPath = ledgerPath;
            }

            if (!string.IsNullOrWhiteSpace(ledgerFolderPath))
            {
                _settings.LedgerFolderPath = ledgerFolderPath;
            }
            
            SaveSettings();
        }

      
    }
}
